package com.ym.jiujiangeyou.service;

import java.util.List;
import java.util.Map;

import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.parse.ParseGson;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.HttpUtil;
import com.ym.jiujiangeyou.util.Util;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 * @Description: 后台获取图片链接  
 */
public class PicUrlService extends Service {
	
	private String flag = null ;
	private int id ;
	private String url = null ;
	
	private MyApplication application = null ;

	private Handler handler = new Handler(){
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.OPTION_SUCCESS:
				Map<String, Object> map  = (Map<String, Object>) msg.obj ;
			//	System.out.println("-----------map----------"+map.size()) ;
			//	application.setUrls((List<String>) map.get("pics")) ;
				if(flag.equals("scenery")){
					application.setSceneryUrlsCache(Util.replaceUrlWithPlus(url+id), (List<String>) map.get("pics")) ;
				}else if(flag.equals("hotel")){
					application.setHotelUrlsCache(Util.replaceUrlWithPlus(url+id), (List<String>) map.get("pics")) ;
				}
				break;
			case Constants.LISTVIEW_ACTION_GET_ERROR:
				Toast.makeText(getApplicationContext(), R.string.check_network_please,
						Toast.LENGTH_LONG).show();
				break ;
			default:
				break;
			}
			
		}
	} ;
	
	public PicUrlService() {
		application = MyApplication.getInstance() ;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;

	}
	
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		id = intent.getIntExtra("id", 1) ;
		url = intent.getStringExtra("url") ;
		flag = intent.getStringExtra("flag") ;
		//System.out.println("-----------id----------"+id) ;
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String s = HttpUtil.httpGet(url+id) ;
			//	System.out.println("-----------s----------"+s) ;
				Map<String, Object> map = null ;
				try {
					map = ParseGson.parsePic(s);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					stopSelf() ;
				}
				if(map != null){
					int status = (Integer) map.get("status");
				//	System.out.println("-----------status----------"+status) ;
					Message msg = handler.obtainMessage();
					switch (status) {
					case 1:
						msg.obj = map;
						msg.what = Constants.OPTION_SUCCESS;
						handler.sendMessage(msg);
						break;
					case 0:
						msg.what = Constants.LISTVIEW_ACTION_GET_ERROR;
						handler.sendMessage(msg);
						break;
					case Constants.LISTVIEW_ACTION_GET_ERROR:
						msg.what = Constants.LISTVIEW_ACTION_GET_ERROR;
						handler.sendMessage(msg);
						break;
					default:
						break;
					}
				}
				stopSelf() ;
			}
		}).start() ;
	}

}
